package com.example.deepschool.Controller.CourseResource;

import com.example.deepschool.Model.Response.ApiResponse;
import com.example.deepschool.enums.GlobalVariables_String;
import com.example.deepschool.service.composite.CompositeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Tag(name = "课程资源API")
@RestController("CourseResource")
@RequestMapping("/CourseResource")
public class Controller {
	
	// 用户令牌的全局变量
	private final String TOKEN = GlobalVariables_String.TOKEN.getValue();
	
	@Operation(summary = "上传之前查询",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@GetMapping("/isExist")
	public ResponseEntity<Object> isExist (String intactSHA256, String ResourceName, Long courseID, HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		Map<String, Object> map = CompositeService.courseResource.isExist(intactSHA256, ResourceName, courseID, UUID);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "删除一个课程资源",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@DeleteMapping("/delete")
	public ResponseEntity<Object> delete (@RequestParam Long courseID,
	                                      @RequestParam Long courseResourceId,
	                                      HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		CompositeService.courseResource.deleteCourseResource(courseID, courseResourceId, UUID);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body("删除成功")
				       .build();
	}
	
	@Operation(summary = "获取一个课程的资源列表",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@GetMapping("/getCourseResourceByCourseId")
	public ResponseEntity<Object> getCourseResourceByCourseId (@RequestParam Long courseId,
	                                                           HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		com.example.deepschool.Model.CourseResource[] courseResources = CompositeService.courseResource.CourseResourceByCourseId(courseId, UUID);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(courseResources)
				       .build();
	}
	
	@Operation(summary = "上传一个课程资源的一个分片",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@PostMapping("/putChunk")
	public ResponseEntity<Object> putChunk (@RequestParam MultipartFile ChunkFile,
	                                        @RequestParam String intactSHA256,
	                                        @RequestParam String ChunkSHA256,
	                                        @RequestParam Long ChunkNum,
	                                        @RequestParam Long MaxChunkNum,
	                                        @RequestParam String ResourceName,
	                                        @RequestParam Long courseID,
	                                        HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		Map<String, Object> map = CompositeService.courseResource.putChunk(ChunkFile,
				intactSHA256,
				ChunkSHA256,
				ChunkNum,
				MaxChunkNum,
				ResourceName,
				courseID,
				UUID);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "获取一个课程资源的临时下载链接",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@GetMapping("/TempDownloadLink")
	public ResponseEntity<Object> TempDownloadLink (@RequestParam Long courseId,
	                                                @RequestParam String ResourceName,
	                                                @RequestParam String address,
	                                                HttpServletRequest request) {
		String UU_ID = request.getHeader(TOKEN);
		Map<String, Object> map = CompositeService.courseResource.TempDownloadLink(courseId, ResourceName, address, UU_ID);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
}
